require "Common/define"
require "Logic/Config"
require "Common/functions"
UIRankFight = {};
HelperFunc.SetUIEnv(UIRankFight)
local this = UIRankFight;
this.my=nil;
this.rank=nil;
this.ctrlNoRank=nil;
this.mylevel=nil;
this.myrank=nil;
this.fight=nil;
this.myhead=nil;
this.ranklist=nil;
this.myNameGuildCtrl = nil
this.guildtxt =  nil

function UIRankFight.Init()
    this.datalist=logicMgr.NoticeManager.PlayerRankList;
    this.mydata=logicMgr.NoticeManager.selfInfo;
    local name, packageName = getUIName(PanelResNames.UIRankFight);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIRankFight.onCreate(obj)
    close=obj:GetChild("btnClose");
    close.onClick:Add(this.onClose);

    this.ranklist=obj:GetChild("ranklist");


    this.my=obj:GetChild("my");
    this.rank=this.my:GetController("rank");
    this.ctrlNoRank=this.my:GetController("ctrlNoRank");
    this.myname=this.my:GetChild("name");
    this.mylevel=this.my:GetChild("level");
    this.myrank=this.my:GetChild("rank");
    this.fight=this.my:GetChild("fight");
    this.myhead=this.my:GetChild("head"):GetChild("head"):GetChild("HeadIcon");
    this.myframeIcon = this.my:GetChild("head"):GetChild("frame");
    this.myNameGuildCtrl = this.my:GetController("showGuild")
    this.guildtxt =  this.my:GetChild("nameGuild")
    RefreshMy();
    
    this.ranklist.itemRenderer = this.ranklistItemRenderer
   -- this.ranklist:SetVirtual()
    this.ranklist.numItems = #this.datalist
    HelperFunc.ListTranstionPlay(this.ranklist)

    ListenEvent(Config.EventType.FreashRoleInfoOther, this.GotOtherShowInfo)
end
function UICity.GotOtherShowInfo(info)
    showUI(PanelNames.UIPlayerShow, info,nil,false,function()
        UIBottom.onClickTown();
        showUI(PanelNames.UIRanking)
        showUI(PanelNames.UIRankFight);
    end)
end
function UIRankFight.ranklistItemRenderer(index,item)
    index=index+1;
    local data=this.datalist[index];
    local rankCon=item:GetController("rank");
    local ctrlNoRank=item:GetController("ctrlNoRank");
    local name=item:GetChild("name");
    local level=item:GetChild("level");
    local rank=item:GetChild("rank");
    local fight=item:GetChild("fight");
    local headbtn=item:GetChild("head");
    local head=item:GetChild("head"):GetChild("head"):GetChild("HeadIcon");
    local frameIcon = item:GetChild("head"):GetChild("frame");
    local nameGuild = item:GetChild("nameGuild")
    local showGuildCtrl = item:GetController("showGuild")
    ctrlNoRank.selectedIndex=0;
    if  index==1 then
        rankCon.selectedIndex=0;
    elseif index==2 then
        rankCon.selectedIndex=1;
    elseif index==3 then
        rankCon.selectedIndex=2;
    elseif index<=50 then 
        rankCon.selectedIndex=3;
        rank.text=index;
    else
        ctrlNoRank.selectedIndex=1;
    end
    if logicMgr.NoticeManager.selfInfo.playerShowInfo.playerId~=data.playerShowInfo.playerId then     
        head.onClick:Clear();
        headbtn.onClick:Add(function()
            roleInfoHandler.sendreqOtherRoleInfo(data.playerShowInfo.playerId);
        end)
    end
    
    level.text=data.playerShowInfo.level;
    fight.text=getNumString(data.parameter);
    name.text=data.playerShowInfo.name;
    head.url= logicMgr.SkinManager.GetHeroIconBySkinId(tonumber(data.playerShowInfo.pic))--"ui://ICONAtlas/"..logicMgr.HeroManager.GetHeroConfigbyModelId(tonumber(data.playerShowInfo.pic)).f_HeroIcon;
    frameIcon.url = "ui://ICONAtlas/" .. data.playerShowInfo.picFrame;
    if data.playerShowInfo.unionName then
        showGuildCtrl.selectedIndex = 1
        nameGuild.text = "<"..data.playerShowInfo.unionName..">"
    else
        showGuildCtrl.selectedIndex = 0
    end
end
function UIRankFight.SendSpeak()
    local tex=start.text;
    NoticeHandler.ReqEditMsg("tex");
    this.onClose();
end

function UIRankFight.RefreshMy()
  -- this.ranklistItemRenderer(logicMgr.NoticeManager.myRank-1,this.my);
    this.ctrlNoRank.selectedIndex=0;
    if  logicMgr.NoticeManager.myRank==1 then
        this.rank.selectedIndex=0;
    elseif logicMgr.NoticeManager.myRank==2 then
        this.rank.selectedIndex=1;
    elseif logicMgr.NoticeManager.myRank==3 then
        this.rank.selectedIndex=2;
    elseif logicMgr.NoticeManager.myRank==0 then
        this.ctrlNoRank.selectedIndex=1;
    elseif logicMgr.NoticeManager.myRank<=50 then 
        this.rank.selectedIndex=3;
        this.myrank.text=logicMgr.NoticeManager.myRank;
     else
        this.ctrlNoRank.selectedIndex=1;
    end
    this.mylevel.text=this.mydata.playerShowInfo.level;
    this.fight.text=getNumString(logicMgr.HeroManager.GetHerosSumPower())
    this.myname.text=this.mydata.playerShowInfo.name;
    this.myhead.url= logicMgr.SkinManager.GetHeroIconBySkinId(tonumber(this.mydata.playerShowInfo.pic))--"ui://ICONAtlas/"..logicMgr.HeroManager.GetHeroConfigbyModelId(tonumber(this.mydata.playerShowInfo.pic)).f_HeroIcon;
    this.myframeIcon.url = "ui://ICONAtlas/" .. this.mydata.playerShowInfo.picFrame;
    local guidName = logicMgr.GuildManager.getUnionName()
    if guidName ~= '' then
        myNameGuildCtrl.selectedIndex = 1
        guildtxt.text = "<".. guidName..">"
    else
        myNameGuildCtrl.selectedIndex = 0
    end
end


function UIRankFight.refreshAll()


end

function UIRankFight.onClose()
    closeUI(PanelResNames.UIRankFight);
end

function UIRankFight.OnDestroy()

end

function UIRankFight.OnEnable()
    if this.ranklist~=nil and #this.datalist > 0 then
        this.ranklist:ScrollToView(0);
    end
   
end

function UIRankFight.OnDisable()
    -- close.onClick:remove(this.onClose);
    -- start.onClick:remove(this.SendSpeak);
    -- black.onClick:remove(this.onClose);
end


return UIRankFight;